package com.cjm.algorithm.leetcode;

import com.cjm.algorithm.TreeNode;

public class DiameterOfBinaryTree543 {
    private int res = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return 0;
        }
        maxDepth(root);
        return res;
    }

    private int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        res = Math.max(res, left + right);
        return Math.max(left, right) + 1;
    }

}
